<?php 
/**************************************************/
## Class Name - Model_TRADE (Contains all the functions related trade modules)
## Created By - Techmodi (24/05/2012)
/**************************************************/
class Model_TRADE extends Database
{	
	function Model_TRADE() {			
		$this->trade_cat = TRADE_CATEGORIES;
		$this->user_trades = USER_TRADES;
		$this->order_items = ORDER_ITEMS;
		$this->Database();
	}
	
	/****************************************************/
	// for add trade
	function addTradeCategory($array) {
		$this->InsertData($this->trade_cat, $array);
		$insertId = mysql_insert_id();
		return $insertId;
	}
	// for add trade
	function addOrderItems($array) {
		$this->InsertData($this->order_items, $array);
		$insertId = mysql_insert_id();
		return $insertId;
	}
	## Get trade name by id 
	function getTradeNameById($tradeId){
		$fields = array('trade_category');	
		$tables = array($this->trade_cat);
		$result1 = $this->SelectData($fields,$tables, $where= array("id='".$tradeId."'"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result['trade_category'];
	}
	## function for checking the job is from the current parent cat 
	function checkJobTypesIsUnderTheParentCat($jobid,$parent_id){
		$fields = array();	
		$tables = array($this->trade_cat);
	$result1 = $this->SelectData($fields,$tables, $where= array("id='".$jobid."' AND parent_id = '".$parent_id."'"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result;		
	}
	## List all Trades
	function getAllTradesCategory($pid,$search,$col1='', $limit='',$offset='') {
		$fields = array();	
		$tables = array($this->trade_cat);
		$where= array();
		if(trim($search)!='') {
			$where[] = "trade_category LIKE '%".trim($search)."%'";
		}
		if($pid!='0'){
			$where[] = "parent_id = '".trim($pid)."'";
		}
		else
		{
			$where[] = "parent_id = '0'";
		}
		if($col1=='1')
			$order = array("trade_category DESC");
		else
			$order = array("trade_category ASC");
		$result1 = $this->SelectData($fields,$tables, $where, $order , $group=array(),$limit,$offset,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## List all Trades by userid
	function getAllTradesCategoryByUserId($userid,$pid,$search,$limit='',$offset='') {
		$fields = array();	
		$tables = array($this->trade_cat);
		$where= array();
		if(trim($search)!='') {
			$where[] = "trade_category LIKE '%".trim($search)."%'";
		}
		if($userid!='0'){
			$where[] = "user_id = '".trim($userid)."'";
		}
		if($pid!='0'){
			$where[] = "parent_id = '".trim($pid)."'";
		}
		else
		{
			$where[] = "parent_id = '0'";
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order , $group=array(),$limit,$offset,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	## Get trade details by id
	function getTradeCategoryDetailsById($id) {
		$fields = array();	
		$tables = array($this->trade_cat);
		$result1 = $this->SelectData($fields,$tables, $where= array("id=$id"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result;
	}
	
	## Get trade details by id
	function new_getTradeCategoryDetailsById($id) {
		$fields = array();	
		$tables = array($this->trade_cat);
		$result1 = $this->SelectData($fields,$tables, $where= array("id in ($id)"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result;
	}
	
	## Edit trade by id
	function editTradeCategoryById($array, $Id){
		$this->UpdateData($this->trade_cat,$array,"id",$Id,0);
	}
	
	## Update trade status with multiple ids
	function updateTradeCategoryStatus($ids, $status) {
		$sql = "UPDATE ".$this->trade_cat." SET status='".$status."' WHERE id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	
	## Delete trade category with multiple ids
	function deleteTradeCategory($ids) {
		$sql1 = "DELETE FROM ".$this->trade_cat." WHERE parent_id IN (".$ids.")";
		$sql = "DELETE FROM ".$this->trade_cat." WHERE id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
		$result2= $this->ExecuteQuery($sql1);	 
	}
	
	## Delete trade cat by id
	function deleteTradeCategoryById($id){
		$this->DeleteData($this->trade_cat,"id",$id);
		$this->DeleteData($this->trade_cat,"parent_id",$id);
	}
	
	## Delete jobtype with multiple ids
	function deleteJobTypeCategory($ids) {		
		$sql = "DELETE FROM ".$this->trade_cat." WHERE id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	
	## Delete trade cat by id
	function deleteJobTypeById($id){
		$this->DeleteData($this->trade_cat,"id",$id);		
	}
	
	## List all Trades Category name
	function getAllTradesCategoryWithActiveStatus() {
		$fields = array('');	
		$tables = array($this->trade_cat);
		$where= array("status='1' and parent_id ='0'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("trade_category ASC"), $group=array(),$limit,$offset,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## List all Trades Category name with multiple ids
	function getAllTradesCategoryWithmultipleId($ids) {
		$sql = "SELECT * From ".$this->trade_cat." WHERE status='1' and id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	## List all job type with active
	function getAllJobTypeWithActiveStatus() {
		$fields = array('');	
		$tables = array($this->trade_cat);
		$where= array("status='1' and parent_id !='0'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("trade_category ASC"), $group=array(),$limit,$offset,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## Get trade details by id
	function getTradeCategoryNameById($id) {
		$fields = array('trade_category');	
		$tables = array($this->trade_cat);
		$result1 = $this->SelectData($fields,$tables, $where= array("id=$id"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result['trade_category'];
	}
	
	## Get job type count details by id
	function getCountJobtypeByTradeId($id) {
		$fields = array('count(id) cntjobtype');	
		$tables = array($this->trade_cat);
		$result1 = $this->SelectData($fields,$tables, $where= array("parent_id=$id"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result['cntjobtype'];
	}
	
	
	## checking job type name exist or not 
	function checkJobTypeName($categoryname, $trade_id='') {
		$fields = array();	
		$tables = array($this->trade_cat);
		$where= array("trade_category='".$categoryname."' and parent_id!='0'");		
		
		if($trade_id != '') {
			$where[] = " id!=".$trade_id;	
		}
		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result;
	}
	
	
	
	## Get all job type details by trade id
	function getAllJobtypeByTradeId($id) {
		$fields = array('');	
		$tables = array($this->trade_cat);
		$result1 = $this->SelectData($fields,$tables, $where= array("parent_id='".$id."'"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## Get all job type details by trade id
	function new_getAllJobtypeByTradeId($id) {
		$fields = array('');	
		$tables = array($this->trade_cat);
		$result1 = $this->SelectData($fields,$tables, $where= array("parent_id in ($id)"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## Get all job type details by trade id with active status
	function getAllJobtypeByTradeId1($id) {
		$fields = array('');	
		$tables = array($this->trade_cat);
		$result1 = $this->SelectData($fields,$tables, $where= array("parent_id=$id and status ='1'"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## Get all job type details by trade id with active status
	function new_getAllJobtypeByTradeId1($id) {
		$fields = array('');	
		$tables = array($this->trade_cat);
		if($id)
			$result1 = $this->SelectData($fields,$tables, $where= array("parent_id in ($id) and status ='1'"), $order = array(), $group=array(),$limit = "",0,0); 
		else
		{
			$id='-1';
			$result1 = $this->SelectData($fields,$tables, $where= array("parent_id in ($id) and status ='1'"), $order = array(), $group=array(),$limit = "",0,0); 
		}
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## Get all job type details by trade id with active status
	function getAllJobtypeByTradeIdActiveStatus($id) {
		$fields = array('');	
		$tables = array($this->trade_cat);
		$result1 = $this->SelectData($fields,$tables, $where= array("status='1' and parent_id=$id"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## Get all job type details by trade id with active status
	function new_getAllJobtypeByTradeIdActiveStatus($id) {
		$fields = array('');	
		$tables = array($this->trade_cat);
		$result1 = $this->SelectData($fields,$tables, $where= array("status='1' and parent_id in ($id)"), $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchAll($result1); 
		return $result;
	}
	
	## checking Trade cat name exist or not 
	function checkTradeCatNameExits($categoryname, $trade_id='') {
		$fields = array();	
		$tables = array($this->trade_cat);
		$where= array("trade_category='".$categoryname."'");		
		
		if($trade_id != '') {
			$where[] = " id!=".$trade_id;	
		}
		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result  = $this->FetchRow($result1); 
		return $result;
	}
	
	function getAllSubcats()
	{
		//get all parent cat
		$sql = "select * from getbids_trade_category where parent_id =0 AND status='1' order by trade_category ASC";
		$rows = $this->loadObjectList($sql);
		$newrows = array();
		if($rows)
		{
			foreach($rows as $row)
			{
				//$sql = "select * from getbids_trade_category where parent_id =".$row['id']." AND status='1' AND price!=0 and expired_time!='0' order by trade_category ASC";
				$sql = "select * from getbids_trade_category where parent_id =".$row['id']." AND status='1' order by trade_category ASC";
				$subrows = $this->loadObjectList($sql);
				if($subrows)
				{
					for($i=0;$i<count($subrows);$i++)
						$subrows[$i]['price'] = number_format($subrows[$i]['price'],2);
					$row['total'] = count($subrows);
					$row['data'] = $subrows;
					$newrows[]= $row;
				}
					
			}	
		}
		
		return $newrows;	
	}

	function updateUserTrades($userid,$trades)
	{
		if($trades)
		{
			foreach($trades as $trade)
			{
				$sql = "SELECT expired from getbids_user_trades where user_id=$userid AND trade_id=".$trade['id'];
				$res = $this->loadResult($sql);
				if($res)
				{
					$today = date("Y-m-d");
					$month = strtotime(date("Y-m-d", strtotime($res)) . " +". $trade['expired_time']. " month");
					$month = strftime("%Y-%m-%d",$month); 
					$sql ='update getbids_user_trades set expired="'.$month.'" where user_id="'.$userid.'" AND trade_id='.$trade['id'];
					
				}
				else
				{
					$today = date("Y-m-d");
					$month = strtotime(date("Y-m-d", strtotime($today)) . " +". $trade['expired_time']. " month");
					$month = strftime("%Y-%m-%d",$month); 
					$sql='insert into getbids_user_trades values("",'.$userid.','.$trade['id'].',"'.$month.'")';
				}
				$this->ExecuteQuery($sql);
			}	
		}		
	}

	function getAvailableJobTypesbyUserId($userid)
	{
		$today = date('Y-m-d');
		$sql = "select DISTINCT tr.* from $this->trade_cat tr INNER JOIN $this->user_trades ut ON tr.id = ut.trade_id WHERE tr.status = '1' AND ut.expired > '$today' AND ut.user_id=$userid ORDER BY trade_category ASC";
		return $this->loadObjectList($sql);
	}
}
?>